package com.cloudkinto.appletservice.covershows.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.covershows.AppletNotificationService;
import com.cloudkinto.appletservice.covershows.vo.AppletNotificationPageRes;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.NotificationDao;
import com.cloudkinto.entity.NotificationDo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-06-19
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletNotificationServiceImpl extends ServiceImpl<NotificationDao, NotificationDo> implements AppletNotificationService {
    @Autowired
    private NotificationDao dao;







    @Override
    public PageResult getListPage(Map<String, String> map) {
        String userId = map.get("userId");
        QueryWrapper<NotificationDo> wrapper = queryBuild(map);
        IPage<NotificationDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<AppletNotificationPageRes> resList = handlerListAfter(page.getRecords(), userId);
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<AppletNotificationPageRes> handlerListAfter(List<NotificationDo> list, String userId) {
        List<AppletNotificationPageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(notificationDo -> {
                AppletNotificationPageRes res = new AppletNotificationPageRes();
                res.setId(notificationDo.getId());
                res.setContent(notificationDo.getContent());
                res.setCreateTime(notificationDo.getCreateTime());
                res.setTitle(notificationDo.getTitle());

                if (StringUtils.isNotBlank((userId))) {
                    if(StringUtils.isNotBlank(notificationDo.getReadUserId()) && notificationDo.getReadUserId().contains(userId)){
                        res.setIsRead(1);
                    }else {
                        res.setIsRead(0);
                    }
                }
                result.add(res);
            });
        }
        return result;
    }

    private IPage<NotificationDo> pageInit(Map<String, String> map) {
        IPage<NotificationDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 5);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<NotificationDo> queryBuild(Map<String, String> map) {
        QueryWrapper<NotificationDo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        return wrapper;
    }


}
